package com.tool.minio.controller;

import com.tool.minio.entity.Media;
import com.tool.minio.service.MediaService;
import com.tool.minio.service.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @author 酒徒
 * @Version 1.0
 * @data 2025/6/19 14:46
 */
@RestController
@RequestMapping("/media")
public class MediaController {
    @Autowired
    private MediaService mediaService;
    @Autowired
    private MinioService minioService;

    @GetMapping()
    public List<Media> findMedia(@PathParam("type") String type) {
        List<Media> list = mediaService.findAllMedia(type);
        return list;
    }

    @PostMapping("/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("type") String type) {
        try {
            String url = minioService.uploadFile(file);

            Media media = new Media();
            media.setUrl(url);
            media.setType(type);
            mediaService.save(media);
            return "sucess";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }
}
